﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using VehicleOOBWeb.Models;
using VehicleOOBWeb.Filters;

namespace VehicleOOBWeb.Controllers
{
    [InitializeSimpleMembership]
    public class VehicleController : Controller
    {
        //
        // GET: /Vehicle/
        
        public ActionResult Index(int manufacturer_code, int? page)
        {
            if (Request.HttpMethod != "GET")
            {
                if (!page.HasValue)
                {
                    page = 1;
                }
            }
            int pageNumber = (page ?? 1);
            ViewBag.Manu = ManufacturerService.GetManufacturerByCode(manufacturer_code);
            return View(VehicleService.GetAllVehiclesByManufacturerCode(manufacturer_code, pageNumber));
        }

        //
        // GET: /Vehicle/Details/5

        public ActionResult Details(int vehicle_id)
        {
            Vehicle vehicle = VehicleService.GetVehicleByID(vehicle_id);
            return View(vehicle);
        }

        //
        // GET: /Vehicle/Create

        public ActionResult Create()
        {
            ViewBag.Manufacturers = ManufacturerService.GetAllManufacturers();
            ViewBag.VehicleTypes = new List<string>() { "Bus", "Car", "Loader", "Tractor", "Truck" };
            return View();
        }

        public ActionResult Search(int? page, string vehicleType, int? manufacturerCode, int? priceFilter, string vehicleName, FormCollection collection)
        {
            if (Request.HttpMethod != "GET")
            {
                if (!page.HasValue)
                {
                    page = 1;
                }
            }
            int pageNumber = (page ?? 1);

            string _vehicleType = collection["vehicleType"];
            int _manufacturerCode = Convert.ToInt32(collection["manufacturerCode"]);
            int _priceFilter = Convert.ToInt32(collection["priceFilter"]);
            string _vehicleName = collection["vehicleName"];

            if (!string.IsNullOrEmpty(vehicleType))
            {
                _vehicleType = vehicleType;
            }

            if (manufacturerCode.HasValue)
            {
                _manufacturerCode = manufacturerCode.Value;
            }

            if (priceFilter.HasValue)
            {
                _priceFilter = priceFilter.Value;
            }

            if (!string.IsNullOrEmpty(vehicleName))
            {
                _vehicleName = vehicleName;
            }

            ViewBag.CboVehicleType = _vehicleType;
            ViewBag.CboManufacturerCode = _manufacturerCode;
            ViewBag.CboPriceFilter = _priceFilter;
            ViewBag.CboVehicleName = _vehicleName;

            return View(VehicleService.GetVehiclesByConditions(_vehicleType, 
                _manufacturerCode, 
                _priceFilter, 
                _vehicleName, 
                pageNumber));
        }

        //
        // POST: /Vehicle/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Vehicle m_vehicle = new Vehicle();
                    UpdateModel(m_vehicle);
                    object sub_vehicle = null;
                    VehicleType sub_type = VehicleType.None;
                    switch (collection["vehicle_type"])
                    {
                        case "Bus":
                            sub_type = VehicleType.Bus;
                            sub_vehicle = new Bus();
                            break;
                        case "Car":
                            sub_type = VehicleType.Car;
                            sub_vehicle = new Car();
                            break;
                        case "Loader":
                            sub_type = VehicleType.Loader;
                            sub_vehicle = new Loader();
                            break;
                        case "Tractor":
                            sub_type = VehicleType.Tractor;
                            sub_vehicle = new Tractor();
                            break;
                        case "Truck":
                            sub_type = VehicleType.Truck;
                            sub_vehicle = new Truck();
                            break;
                        default:
                            break;
                    }
                    if (VehicleService.AddVehicle(m_vehicle, sub_vehicle, sub_type))
                    {
                        return RedirectToAction("Manager");
                    }
                    else
                    {
                        return View("Views/Shared/Error");
                    }
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Vehicle/Edit/5

        public ActionResult Edit(int id)
        {
            ViewBag.Manufacturers = ManufacturerService.GetAllManufacturers();
            ViewBag.VehicleTypes = "";
            Vehicle vehicle = VehicleService.GetVehicleByID(id);
            if (vehicle.Bus != null)
            {
                ViewBag.VehicleTypes = "Bus";
            }
            else if (vehicle.Car != null)
            {
                ViewBag.VehicleTypes = "Car";
            }
            else if (vehicle.Loader != null)
            {
                ViewBag.VehicleTypes = "Loader";
            }
            else if (vehicle.Tractor != null)
            {
                ViewBag.VehicleTypes = "Tractor";
            }
            else if (vehicle.Truck != null)
            {
                ViewBag.VehicleTypes = "Truck";
            }
            return View(vehicle);
        }

        //
        // POST: /Vehicle/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Vehicle m_vehicle = new Vehicle();
                    UpdateModel(m_vehicle);
                    object sub_vehicle = null;
                    VehicleType sub_type = VehicleType.None;
                    switch (collection["vehicle_type"])
                    {
                        case "Bus":
                            sub_type = VehicleType.Bus;
                            sub_vehicle = new Bus();
                            break;
                        case "Car":
                            sub_type = VehicleType.Car;
                            sub_vehicle = new Car();
                            break;
                        case "Loader":
                            sub_type = VehicleType.Loader;
                            sub_vehicle = new Loader();
                            break;
                        case "Tractor":
                            sub_type = VehicleType.Tractor;
                            sub_vehicle = new Tractor();
                            break;
                        case "Truck":
                            sub_type = VehicleType.Truck;
                            sub_vehicle = new Truck();
                            break;
                        default:
                            break;
                    }
                    if (VehicleService.UpdateVehicle(m_vehicle, sub_type))
                    {
                        return RedirectToAction("Manager");
                    }
                    else
                    {
                        return View("Views/Shared/Error");
                    }
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Vehicle/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                VehicleService.DeleteVehicle(id, VehicleType.None);
                return RedirectToAction("Manager");
            }
            catch
            {
                return View();
            }            
        }

        
        public ActionResult Manager(int? page)
        {
            try
            {
                if (Request.HttpMethod != "GET")
                {
                    page = 1;
                }
                int pageNumber = (page ?? 1);
                return View(VehicleService.GetListVehicleByType(VehicleType.None, pageNumber, Config.PageSize));

            }
            catch (Exception)
            {
                return Redirect("Views/Shared/Error");
            }
        }
    }
}
